package com.book.service.impl;

import static com.github.drinkjava2.jsqlbox.DB.pagin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.book.dao.BookInfoDao;
import com.book.entity.BookChapter;
import com.book.entity.BookInfo;
import com.book.model.BookInfoModel;
import com.book.service.itf.IndexServiceItf;
import com.google.common.collect.Maps;

/*
 * 首页服务类
 * 
 * 
 * 
 * 
 *  
 */

 @Service
public class IndexServiceImpl  implements  IndexServiceItf{
	@Autowired
	private BookInfoDao bookDao;
	
	
	
	@Override
	public Map<String, Object> getIndexDataMap() {
		System.out.println("Service中准备调用Dao方法---------------");
		
		List<BookInfo> bookInfos = bookDao.getLatestFour();
		List<BookInfo> dianZanInfos  = bookDao.getZanEight();
		System.out.println("Service中准备调用Dao方法调用结束---------------");
		
		HashMap<String, Object> map = Maps.newHashMap();
		map.put("newBookInfos", bookInfos);
		map.put("dianZanInfos", dianZanInfos);
		
		return map;
	}

	@Override
	public BookInfoModel getBookInfoFromID(Integer book_id){
		BookInfo eo=new BookInfo().loadById(book_id);
 		List<BookChapter> chapterlist = new BookChapter().findAll(" where book_id=" + book_id);
		return new BookInfoModel(eo,chapterlist);
	}

	@Override
	public BookChapter getChapterFormID(Integer chapter_id) {
		
		BookChapter eo = new BookChapter().loadById(chapter_id);
		eo.setChapterText(eo.getChapterText()==null?"":eo.getChapterText());
		eo.setChapterText("<p>" + eo.getChapterText().replace("\n", "</p><p>" + "</p>"));
		return eo;
	}

	@Override
	public List<BookInfo> getRemenList() {
		List<BookInfo> dianZanInfos  = new BookInfo().findBySQL(" select * from book_info order by dian_zan desc",pagin(1,20));

		return dianZanInfos;
	}


}
